// Copyright (C) Kumo inc. and its affiliates.
// Author: Jeff.li lijippy@163.com
// All rights reserved.
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published
// by the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program.  If not, see <https://www.gnu.org/licenses/>.
//

#include <cantor/common/log.h>

namespace cantor::common {

    std::string LogLocation::short_filename(std::string_view file, int depth) {
        int cnt = 0;
        for (int i = file.size() - 1; i >= 0; i--) {
            if (file[i] == '/') {
                cnt++;
            }
            if (cnt == depth) {
                return std::string(file.substr(i + 1));
            }
        }
        return std::string(file);
    }

    void IRLogger::log(LogLocation &&loc, pb::LogLevel level, pb::LogPhase phase, std::string &&pass, std::string &&msg) {
        pb::LogMessage log_message;
        log_message.set_level(level);
        log_message.set_phase(phase);
        log_message.set_file(std::move(loc.file));
        log_message.set_line(loc.line);
        log_message.set_function(std::move(loc.function));
        log_message.set_msg(std::move(msg));
        log_message.set_pass(std::move(pass));
        logs_.push_back(std::move(log_message));
    }
}  // namespace cantor::common
